angular.module('start.home.ctrl', [])
  /*
  app 需要引入服务
  $$cordovaGeolocation
   */
  .controller('HomeCtrl', function($scope, $$AuthService, $ionicScrollDelegate, $ionicModal, $location, $$GuideService, $ionicLoading, $timeout, $ionicSlideBoxDelegate, $rootScope, $$WeixinConfigService, $$CommonService, $$ArticleService, $$CityService, $$cordovaGeolocation) {
    console.log("homeCtrl");

    // 我的分类 数据
    $scope.guideData_rm = [];
    // 我的分类 数据
    $scope.guideData_gz = [];
    // 我的分类 数据
    $scope.guideData_fj = [];

    // 轮播图数据
    // type  1.app内页面路径
    // type  2.iframe外部链接
    // type  3.iframe外部链接 显示复制链接、微信分享
    $scope.ads = [{
      id: 1,
      link: 'article/3',
      type: 0,
      pic: "img/bn001.png",
      name: "",
      sort: 0,
    }];

    $$CommonService.getAds()
      .then(function(res) {
        $scope.ads = res.data;
        $ionicSlideBoxDelegate.update();
      });

    $scope.openadmesg = function(item) {
      console.log(item);
      if (item.type == 1) {
        $location.path(item.link);
      } else if (item.type == 2 || item.type == 3) {
        // console.log("iframe/" + encodeURIComponent(item.link));
        $location.path("iframe/" + encodeURIComponent(item.type) + "/" + encodeURIComponent(item.name) + "/" + encodeURIComponent(item.link));
      }
    };


    $scope.pageData = {
      isloading_rm: true,
      pageSize_rm: 4,
      pageNum_rm: 1,
      nomoreData_rm: false,

      isloading_gz: true,
      pageSize_gz: 4,
      pageNum_gz: 1,
      nomoreData_gz: false,

      isloading_fj: true,
      pageSize_fj: 4,
      pageNum_fj: 1,
      nomoreData_fj: false
    };

    // 我的分类 当前分类
    $scope.mycateno = 1;
    $scope.serviceKinds = null;

    $scope.mycate = function(mycateid) {
      if ($scope.mycateno !== mycateid) {
        $scope.mycateno = mycateid;
      } else {
        return;
      }
      if ($scope.mycateno == 1) {
        // 热门
        $scope.pageData.isloading_rm = true;
        $scope.pageData.pageSize_rm = 4;
        $scope.pageData.pageNum_rm = 1;
        $scope.pageData.nomoreData_rm = false;
        $scope.guideData_rm = [];
        $scope.getHomeGuideList_rm();
      } else if ($scope.mycateno == 2) {
        // 关注
        if ($rootScope.isLogined() == false) {
          $ionicLoading.show({
            template: "请登录",
            noBackdrop: true,
            duration: 1500
          });
        } else {
          $scope.pageData.isloading_gz = true;
          $scope.pageData.pageSize_gz = 4;
          $scope.pageData.pageNum_gz = 1;
          $scope.pageData.nomoreData_gz = false;
          $scope.guideData_gz = [];
          $scope.getHomeGuideList_gz();
        }
      } else
      if ($scope.mycateno == 3) {
        // 附近
        $scope.pageData.isloading_fj = true;
        $scope.pageData.pageSize_fj = 4;
        $scope.pageData.pageNum_fj = 1;
        $scope.pageData.nomoreData_fj = false;
        $scope.guideData_fj = [];
        $scope.getHomeGuideList_fj();
      }
    };

    $scope.loadMore_rm = function() {
      // console.log("loadMore");
      if ($scope.pageData.nomoreData_rm === false && !$scope.pageData.isloading_rm) {
        $scope.getHomeGuideList_rm();
      } else {
        $scope.$broadcast('scroll.infiniteScrollComplete');
      }
    };
    $scope.loadMore_gz = function() {
      // console.log("loadMore");
      if ($scope.pageData.nomoreData_gz === false && !$scope.pageData.isloading_gz) {
        $scope.getHomeGuideList_gz();
      } else {
        $scope.$broadcast('scroll.infiniteScrollComplete');
      }
    };
    $scope.loadMore_fj = function() {
      // console.log("loadMore");
      if ($scope.pageData.nomoreData_fj === false && !$scope.pageData.isloading_fj) {
        $scope.getHomeGuideList_fj();
      } else {
        $scope.$broadcast('scroll.infiniteScrollComplete');
      }
    };

    // 获取向导列表数据
    $scope.getHomeGuideList_rm = function() {
      $scope.pageData.isloading_rm = true;
      // 获取向导列表数据
      $$GuideService.getGuideList($rootScope.config.city.name, $scope.pageData.pageNum_rm, $scope.pageData.pageSize_rm, $scope.serviceKinds)
        .then(function(res) {
          if (res.errCode === 203) {
            $scope.guideData_rm = $scope.guideData_rm.concat(res.data.list);
            $scope.$broadcast('scroll.infiniteScrollComplete');
            // 如果没有更多数据 数据到底了
            $scope.pageData.pageNum_rm++;
            if (res.data.list.length < $scope.pageData.pageSize_rm) {
              $scope.pageData.nomoreData_rm = true;
            } else {
              $scope.pageData.nomoreData_rm = false;
            }
          } else {
            $ionicLoading.show({
              template: res.msg,
              noBackdrop: true,
              duration: 1500
            });
          }
          $scope.pageData.isloading_rm = false;

        }, function() {
          console.info();
          $scope.pageData.isloading_rm = false;
        });
    };
    $scope.getHomeGuideList_rm();

    // 获取关注的向导列表数据
    $scope.getHomeGuideList_gz = function() {
      $scope.pageData.isloading_gz = true;
      // 获取关注向导列表数据
      $$GuideService.getAttentionList($rootScope.config.city.name, $scope.pageData.pageNum_gz, $scope.pageData.pageSize_gz, $scope.serviceKinds)
        .then(function(res) {
          if (res.errCode === 203) {
            if (res.data == undefined) {
              res.data = [];
            }
            $scope.guideData_gz = $scope.guideData_gz.concat(res.data);
            $scope.$broadcast('scroll.infiniteScrollComplete');
            // 如果没有更多数据 数据到底了
            $scope.pageData.pageNum_gz++;
            if (res.data.length < $scope.pageData.pageSize_gz) {
              $scope.pageData.nomoreData_gz = true;
            } else {
              $scope.pageData.nomoreData_gz = false;
            }
          } else {
            $ionicLoading.show({
              template: res.msg,
              noBackdrop: true,
              duration: 1500
            });
          }
          $scope.pageData.isloading_gz = false;

        }, function() {
          console.info();
          $scope.pageData.isloading_gz = false;
        });
    };

    // 获取附近的向导列表数据
    $scope.getHomeGuideList_fj = function() {
      $scope.pageData.isloading_fj = true;
      // 获取向导列表数据
      $$GuideService.getArroundList($rootScope.config.city.name, $scope.pageData.pageNum_fj, $scope.pageData.pageSize_fj, $scope.serviceKinds)
        .then(function(res) {
          if (res.errCode === 203) {
            if (res.data == undefined) {
              res.data = [];
            }
            $scope.guideData_fj = $scope.guideData_fj.concat(res.data);
            $scope.$broadcast('scroll.infiniteScrollComplete');
            // 如果没有更多数据 数据到底了
            $scope.pageData.pageNum_fj++;
            if (res.data.length < $scope.pageData.pageSize_fj) {
              $scope.pageData.nomoreData_fj = true;
            } else {
              $scope.pageData.nomoreData_fj = false;
            }
          } else {
            $ionicLoading.show({
              template: res.msg,
              noBackdrop: true,
              duration: 1500
            });
          }
          $scope.pageData.isloading_fj = false;

        }, function() {
          console.info();
          $scope.pageData.isloading_fj = false;
        });
    };

    // $scope.selectMyCity = function() {
    //   // $location.path("cityselect");
    //   $scope.homecitypickmodal.show();
    // };

    $scope.goguidehotcate = function(id) {
      $location.path("tab/guide/" + id);
    };



    $scope.homecitypickmodal = {};
    $ionicModal.fromTemplateUrl('homecitypicker.html', {
      scope: $scope,
      animation: 'slide-in-top'
    }).then(function(modal) {
      $scope.homecitypickmodal = modal;
      // $scope.homecitypickmodal.show();
    });


    //监听离开页面时关闭model
    $scope.$on("$ionicParentView.beforeLeave", function() {
      console.info("TrafficSignCtrl--$ionicView.beforeLeave ");
      $scope.homecitypickmodal.hide();
    });

    $scope.homecitypickmodalshow = function(wayid) {
      $scope.homecitypickmodal.show();

    };

    $scope.closeModal = function() {
      $scope.homecitypickmodal.hide();
    };


    $scope.provincedatalist = [];
    $scope.citydatalist = [];
    $scope.cityselectresult = {
      id: null,
      name: "请选择",
      province: {
        id: null,
        name: null
      },
      city: {
        id: null,
        name: null
      }
    };

    // 获取省份列表
    $$CityService.getprovince()
      .then(function(res) {
        $scope.provincedatalist = res.data;
      });

    $scope.cp_selectprovince = function(province) {
      $scope.cityselectresult.province.id = province.id;
      $scope.cityselectresult.province.name = province.name;
      // 获取城市列表
      $$CityService.getcitys(province.id)
        .then(function(res) {
          $scope.citydatalist = res.data;
          $ionicScrollDelegate.scrollTop(0);
        });
    };

    $scope.cp_selectcity = function(city) {
      $scope.cityselectresult.city.id = city.id;
      $scope.cityselectresult.city.name = city.city;
    };


    $scope.sureSelectCityResult = function() {
      if ($scope.cityselectresult.city.id != null) {
        if ($rootScope.config.city.name != $scope.cityselectresult.city.name) {
          // $scope.mycateno = 1;
          $rootScope.config.city.id = $scope.cityselectresult.city.id;
          $rootScope.config.city.name = $scope.cityselectresult.city.name;
          $scope.pageData = {
            isloading_rm: true,
            pageSize_rm: 4,
            pageNum_rm: 1,
            nomoreData_rm: false,

            isloading_gz: true,
            pageSize_gz: 4,
            pageNum_gz: 1,
            nomoreData_gz: false,

            isloading_fj: true,
            pageSize_fj: 4,
            pageNum_fj: 1,
            nomoreData_fj: false
          };
          $scope.guideData_rm = [];
          $scope.getHomeGuideList_rm();
          $scope.guideData_gz = [];
          if ($rootScope.isLogined() == true) {
            $scope.getHomeGuideList_gz();
          }
          $scope.guideData_fj = [];
          $scope.getHomeGuideList_fj();
        } else {
          $rootScope.config.city.id = $scope.cityselectresult.city.id;
          $rootScope.config.city.name = $scope.cityselectresult.city.name;
        }
      }
      $scope.homecitypickmodal.hide();
    };

    $scope.reselectprovince = function() {
      $scope.cityselectresult.province.id = null;
      $scope.cityselectresult.province.name = null;
      $scope.cityselectresult.city.id = null;
      $scope.cityselectresult.city.name = null;
    };

    $scope.openCitySelectModel = function() {
      $scope.homecitypickmodal.show();
    };

    $scope.comingsoon = function() {
      $ionicLoading.show({
        template: "敬请期待",
        noBackdrop: true,
        duration: 1500
      });
    };

    $scope.toyjhf = function() {
      var item = {
        type: 3,
        link: "http://web.2blab.com/dcyy",
        name:"云见何方"
      };
      $rootScope.toiframepage(item);
    };

  });
